import Common from '../common'
var Js = Common('child')
export default {
  name: 'child',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'child_no',
          label: '推广用户NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入推广用户NO',
            clearable: true,
            defaultValue: '',
            optionDataName: '',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.child_no = ['LIKE',['%'+val+'%']];
              } else {
                delete self.searchItem.child_no;
              };
            },
          },
        },
        {
          key: "login_name",
          label: '推广用户手机号',
          application: ['添加账号', '编辑账号'],
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val){
            return val.user.login_name
          }
        },
        {
          key: 'nickname',
          label: '推广用户昵称',
          application: ['编辑账号', '添加账号'],
          componentName: 'sls-input',
          listType: 'normal',
          placeholder: '请输入昵称',
          formatter: function(val){
            return val.user.nickname
          }
        },
        {
          key: 'headImgUrl',
          label: '推广用户头像',
          application: ['编辑账号'],
          componentName: 'upload',
          needStr: true,
          dialogStyle: 'width:100%;',
          customSlot: 'headImgUrl',
          listType: 'normal',
        },
        {
          key: 'level',
          label: '推广等级',
          application: ['编辑账号', '添加账号'],
          listType: 'normal',
          placeholder: '请输入推广等级',
          componentName: 'sls-select',
          optionsName: 'levelOptions',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          formatter: function(val){
            return val.level+'级'
          },
          header_search: {
            componentName: 'sls-select',
            optionsName: 'levelOptions',
            placeholder: '请选择推广等级',
            changeFunc: function(val, self) {
              if (val || val === 0) {
                self.searchItem.level = val;
              } else {
                delete self.searchItem.level;
              };
            },
          }
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };

            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 120
        },
      ],
      // 按钮配置
      btn_info: [
        {
          type: 'danger',
          icon: 'delete',
          size: 'mini',
          funcType: 'submit',
          position: 'header',
          text: function(data) {
            return '删除选中'
          },
          func: {
            apiName: function(self) {
              return "api_userUpdate"
            },
            postData: function(self) {
              var deleteArray = [];
              for (var i = 0; i < self.selectionArray.length; i++) {
                deleteArray.push(self.selectionArray[i].id);
              };
              var postData = {
                searchItem: {
                  id: ['in', deleteArray],
                  user_type: 0
                },
                data: {
                  status: -1
                }
              };
              return postData;
            }
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'header',
          noAuth: true,
          text: function(data) {
            return '返回'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              var fullPath = self.$route.fullPath;
              var tabList = self.$store.state.tabs.tabs;
              var routerRes = self.$$getBackRoute(tabList, fullPath);
              self.$store.commit('REMOVE_TABS', {
                fullPath: fullPath,
              });
              if (routerRes.backRes) {
                self.$router.push(routerRes.backRes);
              } else {
                self.$router.push(routerRes.newRes);
              };
            },
          },
        },
        {
          type: 'info',
          icon: 'edit',
          size: 'mini',
          position: 'list',
          text: function(data) {
            return '订单列表'
          },
          styleFunc: function(data) {
            return 'background-color: #396edf;border-color:#396edf'
          },
          funcType: 'func',
          func: {
            func: function(self) {
              self.$router.push({
                path: '/home/xorder',
                name: '订单列表',
                params: {
                  type: 2,
                  user_no: self.originFormData.child_no,
                  parent_no: self.parent_no,
                  delKeepAlive: true,
                  backPath: '/home/staff'
                }
              });
            },
          },
        }
      ],


      searchItem: {
        level: ['in',[1,2]]
      },
      startSearchItem: {
        level: ['in',[1,2]]
      },
      optionData: {
        labelOptions: [],
        statusOptions: [{
          text: '启用',
          value: 1
        }, {
          text: '待审核',
          value: 2
        }, {
          text: '禁用',
          value: -1
        }],
        levelOptions: [{
            text: '1级',
            value: 1
          },
          {
            text: '2级',
            value: 2
          },
        ],
        roleOptions: [],
        typeOptions: [{
            text: '余额',
            value: 2
          },
        ],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },
      orderSearchItem: {
        pay_status: 1,
        level: 1
      }
    }

  },

  mounted() {
    const self = this;
    self.parent_no = this.$route.params.parent_no;
    self.backPath = this.$route.params.backPath;
    self.searchItem.parent_no = self.parent_no;
    self.startSearchItem.parent_no = self.parent_no;
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  watch: {

  },
  methods: {

    /**
     * 初始化
     */
    init() {
      this.initMainData();
    },

    async excelOutput(self) {

      if (self.btnName == '导出') {
        const postData = {};
        postData.token = self.$store.getters.getToken;
        if (self.searchItem) {
          postData.searchItem = self.$$cloneForm(self.searchItem)
        };
        postData.excelOutput = {
          expTitle: 'test',
          expCellName: [



            {
              title: '姓名',
              key: ['info', 'name'],
              type: 'string',
            },
            {
              title: '地址',
              key: ['info', 'address'],
              type: 'string',
            },
            {
              title: '电话',
              key: ['info', 'phone'],
              type: 'string',
            },
            {
              title: '性别(1：男 2：女)',
              key: ['info', 'gender'],
              type: 'string',
            },
            {
              title: '年龄',
              key: ['info', 'age'],
              type: 'string',
            },
            {
              title: '购物次数',
              key: ['order', 'count'],
              type: 'string',
            },
            {
              title: '消费金额',
              key: ['order', 'price'],
              type: 'string',
            },
            {
              title: '消费时间跨度',
              key: ['orderDay'],
              type: 'string',
            },
          ],
          fileName: '用户列表'
        };

        window.open('http://42.193.100.244/155/public/index.php/api/v1/Base/User/get?token=' +
          postData.token +
          '&searchItem=' +
          JSON.stringify(postData.searchItem) +
          '&excelOutput=' +
          JSON.stringify(postData.excelOutput))
      }
    },

    async initLevelData() {

      const self = this;
      const postData = {};

      postData.token = self.$store.getters.getToken;
      postData.searchItem = {
        menu_id: 21
      };
      var res = await self.$$api_articleGet({
        data: postData
      });
      self.optionData.levelOptions = res.info.data;
      self.optionData.levelOptions.push({
        title: '普通会员',
        listorder: 0
      })
    },

    /**
     * 列表主函数
     */
    async initMainData() {

      const self = this;
      const postData = {};
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      if (self.searchItem) {
        postData.searchItem = self.$$cloneForm(self.searchItem)
      };
      if (JSON.stringify(self.getBefore) != "{}") {
        postData.getBefore = self.$$cloneForm(self.getBefore);
      };
      postData.getBefore = {
        user: {
          tableName: 'User',
          middleKey: 'child_no',
          key: 'user_no',
          condition: 'in',
          searchItem: {
            status: ['in',[1]]
          }
        }
      };
      postData.getAfter = {
        user: {
          tableName: 'User',
          middleKey: 'child_no',
          key: 'user_no',
          condition: '=',
          info: ['login_name','role','user_no','nickname','status'],
          searchItem: {
            status: ['in',[1,-1]]
          }
        }
      };
      var res = await self.$$api_distributionGet({
        data: postData
      });

      self.mainData = res.info.data;

      self.mainData.forEach((item,index) => {
        //forEach循环
        // if(item.headImgUrl){
        //   self.mainData[index]['headImgUrl'] = [{'url':item.headImgUrl}];
        // };
      });


      self.paginate.count = res.info.total;

    },

  },


}
